goog.provide('dkpoints.menu');

goog.require('goog.dom');
goog.require('goog.events');

// Header image list.
dkpoints.menu.headerImgs = [];
  dkpoints.menu.headerImgs[0] = "shopping-cart-icon.png";
  dkpoints.menu.headerImgs[1] = "credit-card-icon.png";
  dkpoints.menu.headerImgs[2] = "cash-register-icon.png";
  dkpoints.menu.headerImgs[3] = "coins-icon.png";
  dkpoints.menu.headerImgs[4] = "Money-Pig-1-icon.png";

dkpoints.menu.init = function()
{
  var me = dkpoints.menu;
  
  // Pick a random header
  var ind = Math.round(Math.random() * 10) % dkpoints.menu.headerImgs.length;
  var imgUrl = "/images/" + dkpoints.menu.headerImgs[ind];
  
  var head = goog.dom.$('header_content');
  head.style.backgroundImage = "url('" + imgUrl + "')";
  head.style.backgroundRepeat = "no-repeat;";
  
  // Add menu mouseover (for Admin) and click listeners
  var adminLink = goog.dom.$('adminMenu');
  var menuTooltip = goog.dom.$('menu_tooltip');
  
  goog.events.listen(adminLink, goog.events.EventType.MOUSEOVER, me.showAdminMenu);
  goog.events.listen(adminLink, goog.events.EventType.MOUSEOUT, me.hideAdminMenu);
  
  goog.events.listen(menuTooltip, goog.events.EventType.MOUSEOVER, me.showAdminMenu);
  goog.events.listen(menuTooltip, goog.events.EventType.MOUSEOUT, me.hideAdminMenu);
}

// Methods for the Admin menu show/hide
dkpoints.menu.showAdminMenu = function()
{
  var tooltip = goog.dom.$("menu_tooltip");
  var adminArrow = goog.dom.$("adminMenu");
  
  var me = dkpoints.menu;
  
  var x = me.getScreenTopLeft(adminArrow).x;
  var y = me.getScreenTopLeft(adminArrow).y + adminArrow.offsetHeight;
  
  tooltip.style.top = y + "px";
  tooltip.style.left = x + "px"; 
  tooltip.style.display = "block";
}

dkpoints.menu.hideAdminMenu = function()
{
  goog.dom.$("menu_tooltip").style.display = "none";
}

// Helper method to decide where to put the admin menu
dkpoints.menu.getScreenTopLeft = function (obj)
{
  var x = obj.offsetLeft;
  var y = obj.offsetTop;

  if(obj.offsetParent)
  {
    var parOffset = dkpoints.menu.getScreenTopLeft(obj.offsetParent);
    x += parOffset.x;
    y += parOffset.y;
  }

  return {x:x, y:y};
}

// Function for displaying/hiding the Admin main menu link after logging in/out.
// Takes a reference to the authStatus object.
dkpoints.menu.toggleAdmin = function(authStatus)
{
  if(authStatus['loggedIn']) {
    if(authStatus['isAdmin']) {
      goog.dom.$('adminLinkSpan').style.display = "inline";
    }
    else {
      goog.dom.$('adminLinkSpan').style.display = "none";
    }
    
    goog.dom.$('user').innerHTML = 'Logged in as: ' + authStatus['name'] + ' <a href="#/auth/logout">(Log out)</a>';
  }
  else
  {
    goog.dom.$('adminLinkSpan').style.display = "none";
    goog.dom.$('user').innerHTML = '<a href="#/auth/login">User Login</a>';
  }
};